{% extends "base.html" %}

{% block title %}新建项目{% endblock %}

{% block content %}
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h4 class="mb-0">新建项目</h4>
                </div>
                <div class="card-body">
                    <form method="post" onsubmit="return validateForm()">
                        <div class="mb-3">
                            <label for="name" class="form-label">项目名称</label>
                            <input type="text" class="form-control" id="name" name="name" 
                                   maxlength="128" required
                                   pattern=".{2,128}"
                                   oninput="this.setCustomValidity('')"
                                   oninvalid="this.setCustomValidity('项目名称长度必须在2-128个字符之间')"
                                   value="{{ form_data.name or '' }}">
                            <div class="form-text">项目名称长度在2-128个字符之间</div>
                        </div>
                        <div class="mb-3">
                            <label for="project_code" class="form-label">项目编号</label>
                            <input type="text" class="form-control" id="project_code" 
                                   name="project_code" maxlength="64" required
                                   pattern="[A-Za-z0-9]{4,64}"
                                   oninput="this.setCustomValidity('')"
                                   oninvalid="this.setCustomValidity('项目编号只能包含字母和数字，长度在4-64个字符之间')"
                                   value="{{ form_data.project_code or '' }}">
                            <div class="form-text">只能包含字母和数字，长度在4-64个字符之间</div>
                        </div>
                        <div class="mb-3">
                            <label for="project_type" class="form-label">项目类型</label>
                            <select class="form-select" id="project_type" name="project_type" required>
                                <option value="">请选择项目类型</option>
                                <option value="industrial" {% if form_data.project_type == 'industrial' %}selected{% endif %}>工业测控</option>
                                <option value="environmental" {% if form_data.project_type == 'environmental' %}selected{% endif %}>环境监测</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">项目规模</label>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="input-group">
                                        <input type="number" class="form-control" 
                                               id="monitoring_points" name="monitoring_points" 
                                               placeholder="监测点数" required min="1" max="9999"
                                               value="{{ form_data.monitoring_points or '' }}">
                                        <span class="input-group-text">个</span>
                                    </div>
                                    <div class="form-text">监测点数量必须在1-9999之间</div>
                                </div>
                                <div class="col-md-6">
                                    <div class="input-group">
                                        <input type="number" class="form-control" 
                                               id="control_loops" name="control_loops" 
                                               placeholder="控制回路数量" required min="1" max="9999"
                                               value="{{ form_data.control_loops or '' }}">
                                        <span class="input-group-text">个</span>
                                    </div>
                                    <div class="form-text">控制回路数量必须在1-9999之间</div>
                                </div>
                            </div>
                        </div>
                        <div class="text-end">
                            <a href="{{ url_for('main.projects') }}" class="btn btn-secondary">
                                <i class="fas fa-times me-1"></i>取消
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-check me-1"></i>创建项目
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function validateForm() {
    // 验证项目名称
    var name = document.getElementById('name').value.trim();
    if (name.length < 2 || name.length > 128) {
        alert('项目名称长度必须在2-128个字符之间');
        return false;
    }

    // 验证项目编号
    var projectCode = document.getElementById('project_code').value.trim();
    if (projectCode.length < 4 || projectCode.length > 64 || !/^[A-Za-z0-9]+$/.test(projectCode)) {
        alert('项目编号只能包含字母和数字，长度在4-64个字符之间');
        return false;
    }

    // 验证项目类型
    var projectType = document.getElementById('project_type').value;
    if (!projectType) {
        alert('请选择项目类型');
        return false;
    }

    // 验证数值字段
    var monitoringPoints = parseInt(document.getElementById('monitoring_points').value);
    var controlLoops = parseInt(document.getElementById('control_loops').value);
    
    if (isNaN(monitoringPoints) || monitoringPoints < 1 || monitoringPoints > 9999) {
        alert('监测点数量必须在1-9999之间');
        return false;
    }
    
    if (isNaN(controlLoops) || controlLoops < 1 || controlLoops > 9999) {
        alert('控制回路数量必须在1-9999之间');
        return false;
    }

    return true;
}
</script>
{% endblock %} 